package javafx.demo;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.FXButtonUtils;

public class TestForm extends Application {

	Button amiButton;
	Label imageLabel;
	Label otherLabel;
	Button imageButton;
	
	@Override
	public void start(Stage stage) throws Exception {
		amiButton = getAmiationButton();

		imageButton = getImageButton();
		
		imageLabel = getImageLabel();

		otherLabel = getOtherLabel();

		ChoiceBox<Object> cb = getChoiceBox();
		
		VBox vbox = new VBox();
		vbox.setAlignment(Pos.BASELINE_CENTER);
		vbox.setSpacing(40);
		vbox.getChildren().add(amiButton);
		vbox.getChildren().add(getTogglButton());
		vbox.getChildren().add(imageButton);
		vbox.getChildren().add(imageLabel);
		vbox.getChildren().add(otherLabel);
		vbox.getChildren().add(cb);

		BorderPane outerRoot = new BorderPane();
		outerRoot.setCenter(vbox);

		Scene scene = new Scene(outerRoot, 600, 500);

		stage.setTitle("Hello World!");
		stage.setScene(scene);
		stage.show();

	}

	private ChoiceBox<Object> getChoiceBox() {
		ChoiceBox<Object> cb = new ChoiceBox<Object>();
		cb.setItems(FXCollections.observableArrayList(
		    "New Document", "Open ", 
		    new Separator(), "Save", "Save as")
		);
		cb.setTooltip(new Tooltip("Select the language"));
		return cb;
	}

	private Button getImageButton() {
		Image imageOk = new Image(getClass().getResourceAsStream("../image/accept.png"));
		Button imageButton = new Button("Accept", new ImageView(imageOk));
		imageButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		    	otherLabel.setText("Accepted other thing");
		    }
		});
		
		// -fx-font属性为button1设置了字体大小，-fx-base属性改变了按钮默认的颜色。
		// 这样，button就具有了浅绿色和大字体
		imageButton.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
		
		FXButtonUtils.shadow(imageButton);
		
		return imageButton;
	}

	private Label getImageLabel() {
		Image image = new Image(getClass().getResourceAsStream("../image/user.png"));
		Label label = new Label("Search this aminal no problem", new ImageView(image));
		label.setFont(new Font("Arial", 20));
		label.setWrapText(true);// 换行
		return label;
	}

	/**
	 * 转动了270 °并且垂直方向平移了50
	 * 
	 * @return
	 */
	private Label getOtherLabel() {
		Label label = new Label("other thing");
		label.setFont(new Font("Arial", 10));
		label.setRotate(270);
		label.setTranslateY(50);

		// 鼠标在上面时，放大二倍
		label.setOnMouseEntered(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent e) {
				label.setScaleX(2);
				label.setScaleY(2);
			}
		});

		//  离开时还原
		label.setOnMouseExited(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent e) {
				label.setScaleX(1);
				label.setScaleY(1);
			}
		});
		return label;
	}

	private Button getAmiationButton() {
		Button button = new Button();
		button.setText("OK");
		button.setFont(new Font("Tahoma", 24));
		button.setEffect(new Reflection());

		final Timeline timeline = new Timeline();
		timeline.setCycleCount(Timeline.INDEFINITE);
		timeline.setAutoReverse(true);
		final KeyValue kv = new KeyValue(button.opacityProperty(), 0);
		final KeyFrame kf = new KeyFrame(Duration.millis(600), kv);
		timeline.getKeyFrames().add(kf);
		timeline.play();
		return button;
	}

	private HBox getTogglButton(){
		final ToggleGroup group = new ToggleGroup();

		ToggleButton tb1 = new ToggleButton("Minor");
		tb1.setToggleGroup(group);
		tb1.setSelected(true);

		ToggleButton tb2 = new ToggleButton("Major");
		tb2.setToggleGroup(group);

		ToggleButton tb3 = new ToggleButton("Critical");
		tb3.setToggleGroup(group);
		
		tb1.setUserData(Color.LIGHTGREEN);
		tb2.setUserData(Color.LIGHTBLUE);
		tb3.setUserData(Color.SALMON);

		tb1.setStyle("-fx-base: lightgreen;");
		tb2.setStyle("-fx-base: lightblue;");
		tb3.setStyle("-fx-base: salmon;");
		
		final Rectangle rect = new Rectangle(145, 50);
		
		group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
		    public void changed(ObservableValue<? extends Toggle> ov,
		        Toggle toggle, Toggle new_toggle) {
		            if (new_toggle == null)
		                rect.setFill(Color.WHITE);
		            else
		                rect.setFill(
		                    (Color) group.getSelectedToggle().getUserData()
		                );
		         }
		});
		
		HBox hBox = new HBox();
		hBox.getChildren().add(tb1);
		hBox.getChildren().add(tb2);
		hBox.getChildren().add(tb3);
		hBox.getChildren().add(rect);
		return hBox;
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}
